<?php

namespace app\admin\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class Orderlist extends Controller
{
    public function tenantlist()
    {
        $this->db = new SysDb();
        $data = $this->db->table('tenant_info')->lists();
        exit(json_encode(['message' =>'succ','result'=>$data]));
    }
    public function storelist()
    {
        $this->db = new SysDb();
        $data = $this->db->table('goods_store')->lists();
        exit(json_encode(['message' =>'succ','result'=>$data]));
    }
    public function orderlist()
    {
        
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $status  = trim(input('param.status'));
        $busId  = trim(input('param.busId'));
        $tenantId  = trim(input('param.tenantId'));
        $storeId  = trim(input('param.storeId'));
        $orderId  = trim(input('param.orderId'));
        $where=array(
            "bus_id" => $busId,
            "tenant_id" => $tenantId,
            "status" => $status,
            "store_id" =>$storeId,
            "id" =>$orderId,
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $list = DB::name("order_info")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('order_info')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    public function goodlist()
    {
        $this->db = new SysDb();
        $data = $this->db->table('goods_info')->lists();
        exit(json_encode(['message' =>'succ','result'=>$data]));
    }
    public function finsh()
    {
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $data = $this->db->table('order_info')->where(['id' => $id])->item();
        $datak = $this->db->table('pk_info')->where(['order_id' => $id])->item();
        $data['status']=4;
        $datak['status']=2;
        $datas = $this->db->table('order_info')->where(['id' => $id])->update($data);
        $dataks = $this->db->table('pk_info')->where(['order_id' => $id])->update($datak);
        if($datas||$dataks){
            exit(json_encode(['message' =>'succ','result'=>'修改成功']));
        }
        
    }
    public function refund(){
        $id = trim(input('param.id'));
        $this->db = new SysDb();
        $ids=$this->db->table('pk_info')->where(['id' =>  $id])->item();
        $user=$this->db->table('bus_info')->where(['id' =>  $ids['bus_id']])->item();
        $order=$this->db->table('order_info')->where(['id' =>  $ids['order_id']])->item();
        $idg=$this->db->table('order_info')->where(['id' =>  $ids['order_id']])->delete();
        $idp= $this->db->table('pk_info')->where(['id' =>  $id])->delete();
          $tenant = $this->db->table('tenant_goods_price')->where(['goods_id' => $ids['goods_id']])->item();
            $profitg['tenant_id']=$ids['tenant_id'];
            $profitg['deal_type']=2;
            $profitg['origin']="客户退货";
            $profitg['money']=$tenant['tenant_profit'];
            $profitg['remark']="客户退货";
            $profitg['status']=1;
            $this->db->table('tenant_profit')->insert($profitg);
            $profits['deal_type']=2;
            $profits['origin']="客户退货";
            $profits['money']=$tenant['profit'];
            $profits['remark']="客户退货";
            $profits['status']=1;
            $this->db->table('ad_profit')->insert($profits);
            $flow['tenant_id']=$ids['tenant_id'];
            $flow['type']=2;
            $flow['deal_type']=2;
            $flow['deal_desc']="客户退货";
            $flow['money']=$ids['amount'];
            $flow['remark']="客户退货";
            $flow['status']=1;
            $this->db->table('tenant_flow')->insert($flow);
            $flows['type']=2;
            $flows['money']=$tenant['profit'];
            $flows['remark']="客户退货";
            $flows['status']=1;
            $this->db->table('ad_flow')->insert($flows);
            $user['balance']=$user['balance']+$ids['amount'];
            $this->db->table('bus_info')->where(['id' =>  $ids['bus_id']])->update($user);
            $buss["amount"] = $ids['amount'];
            $buss["tenant_id"] =$ids['tenant_id'];
            $buss["bus_id"] = $ids['bus_id'];
            $buss["deal_desc"] ="客户退货";
            $buss["remark"] = "客户退货";
            $buss["type"] = 1;
            $buss["deal_type"] =4;
            $this->db->table('bus_flow')->insert($buss);
            $summany= DB::name("sys_summary")->order("id desc")->find();
            $idw=$summany['id'];
            $arraym=explode(" ",$summany['create_time']);
            if($arraym[0]==date("Y-m-d")){
                $summany['profit']=$summany['profit']-$tenant['profit'];
                $summany['tenant_profit']=$summany['tenant_profit']-$tenant['tenant_profit'];
                $summany['order_num']=$summany['order_num']-1;
                $summany['pk_num']= $summany['pk_num']-$order['pk_count'];
                $this->db->table('sys_summary')->where(['id' => $idw])->update($summany);
            }
            else{
                $summany['id']=$summany['id']+1;
                $summany['tenant_id']=$ids['tenant_id'];
                $summany['bus_num']=0;
                $summany['recharge']=0;
                $summany['order_num']=0;
                $summany['pk_num']=0;
                $summany['profit']=0;
                $summany['tenant_profit']=0;
                $summany['create_time']=date('Y-m-d h:i:s', time());;
                $this->db->table('sys_summary')->insert($summany);
            }
        if($idg||$idp){
            exit(json_encode(['message' =>'succ','result'=>'退款成功']));
        }
       
    }
    public function orderlists()
    {
        $orderId=trim(input('param.orderId'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        if($orderId==''){
            $list = DB::name("pk_info")->order("create_time desc")->limit($tol, $limit)->select();
            foreach ($list as  $k=>&$value) {
                $listk = DB::name("goods_info")->where('id',  $value['goods_id'])->find();
                $value['cover']=$listk['cover'];
            }
            $this->db = new SysDb();
            $count = $this->db->table('pk_info')->count();
            $data = array();
            array_push($data, $list);
            array_push($data, $count);
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
        }else{
        $list = DB::name("pk_info")->order("create_time desc")->where('order_id',  $orderId)->limit($tol, $limit)->select();
        foreach ($list as  $k=>&$value) {
            $listk = DB::name("goods_info")->where('id',  $value['goods_id'])->find();
            $value['cover']=$listk['cover'];
        }
        $this->db = new SysDb();
        $count = $this->db->table('pk_info')->where(['order_id' => $orderId]) ->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    }
}

